package com.xidian.zhiancloud.service.user.usermanage;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.MenuRightManagerMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.RoleManagerMapper;
import com.xidian.zhiancloud.model.syset.MenuRightList;
import com.xidian.zhiancloud.model.syset.RoleList;
import com.xidian.zhiancloud.model.syset.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleService extends ServiceImpl<RoleManagerMapper,RoleList> {

    @Autowired
    RoleManagerMapper roleManagerMapper;

    @Autowired
    MenuRightManagerMapper menuRightManagerMapper;

    @Autowired
    HttpServletRequest request;

    @Autowired
    SysUserMapper usermgMapper;

    public RoleList selectByNameAndCode(Long companyId,String name,String code){
        return roleManagerMapper.selectByNameAndCode(companyId,name,code);
    }

    public void addRolePermissions(Long companyId,String name,String code){
        String ids = request.getParameter("ids");
        roleManagerMapper.addRolePer(companyId,name, code);
        RoleList roleList = roleManagerMapper.selectByNameAndCode(companyId,name, code);
        List<MenuRightList> menuRightList = new ArrayList<>();
        Integer roleId =roleList.getId();
        if (StringUtils.isNotBlank(ids)){
            String[] meunRId = ids.split(";");
            for (String meunRIds : meunRId) {
                String[] meunIdSelected = meunRIds.split(",");
                    String meunId = meunIdSelected[0];
                    Integer selected = Integer.valueOf(meunIdSelected[1]);
                    MenuRightList menuRight = new MenuRightList();
                    menuRight.setMeunId(Integer.valueOf(meunId));
                    menuRight.setRoleId(roleId);
                    menuRight.setSelected(Integer.valueOf(selected));
                    menuRightList.add(menuRight);
            }
            menuRightManagerMapper.addMenuRight(roleId,menuRightList);
        }
    }

    public void updateRolePermissions(Integer id,String name,String code){
        String ids = request.getParameter("ids");
        Integer roleId =id;
        roleManagerMapper.updateRolePermissions(id,name, code);
        List<MenuRightList> menuRightList = new ArrayList<>();
        //RoleList roleList = roleManagerMapper.selectById(id);
        String[] meunRId = ids.split(";");
        for (String meunRIds : meunRId) {
            String[] meunIdSelected = meunRIds.split(",");
            String meunId = meunIdSelected[0];
            Integer selected = Integer.valueOf(meunIdSelected[1]);
            MenuRightList menuRight = new MenuRightList();
            menuRight.setMeunId(Integer.valueOf(meunId));
            menuRight.setRoleId(roleId);
            menuRight.setSelected(Integer.valueOf(selected));
            menuRightList.add(menuRight);
        }
        menuRightManagerMapper.addMenuRight(roleId,menuRightList);
    }

    public void deleteRolePer(Integer id){
        RoleList role=roleManagerMapper.selectById(id);
        Integer roleId = role.getId();
        roleManagerMapper.deleteRolePer(id);
        menuRightManagerMapper.deleteByroleId(roleId);
    }

    public List<RoleList> getRoleList()
    {
        return roleManagerMapper.getRoleList();
    }

    public int insertRoleUser(String roleids,String id){
        List<String> list = getList(id);
        return roleManagerMapper.insertRoleUser(roleids,list);
    }

    /**
     * id放入list
     * @param id id(多个已逗号分隔)
     * @return List集合
     */
    public List<String> getList(String id) {
        List<String> list = new ArrayList<String>();
        String[] str = id.split(",");
        for (int i = 0; i < str.length; i++) {
            list.add(str[i]);
        }
        return list;
    }


    public int deleteRoleUser(String roleid,String id) {
        List<String> list = getList(id);
        List<SysUser> userList = usermgMapper.getByUserIdAndRoleIds(list);
        String roleids = "0";
        List<SysUser> usList= new ArrayList<>();
        for (int i = 0 ;i<userList.size();i++) {
            SysUser user = new SysUser();
            roleids = userList.get(i).getRoleids();
            Long ids = userList.get(i).getId();
            //删除多余字符串
            roleids = roleids.replace(roleid,"");
            user.setRoleids(roleids);
            user.setId(ids);
            usList.add(user);
        }
        return usermgMapper.updateByIdAndRoleIds(usList);
    }

}
